﻿/*
' Copyright (c) 2012-2013 Kyriakos Lesgidis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using net.diadrasis.CameraSlideshow.Components;
using System.Data;
using System.Web.UI.WebControls;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Common;

namespace net.diadrasis.CameraSlideshow
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditCameraSlideshow class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from CameraSlideshowModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class EditItem : CameraSlideshowModuleBase
    {

        public DataTable image;

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------

        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                image = new DataTable();
                image.Load(cMediaObjectController.getMedia(Convert.ToInt32(Request.QueryString["Id"])));

                if (!Page.IsPostBack)
                {                    
                    Label4.Text = getImageUrl(image.Rows[0]["mediaObjectUrl"].ToString());
                    teCaption.Text = image.Rows[0]["mediaObjectCaption"].ToString();
                    this.txtTarget.Text = image.Rows[0]["mediaObjectTarget"].ToString();                
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        public string getImageUrl(string fID)
        {
            if (fID != "")
            {
                FileController fileCnt = new FileController();
                FileInfo objFile = fileCnt.GetFileById(Convert.ToInt32(fID.Substring(7)), PortalId);

                return Globals.GetPortalSettings().HomeDirectory + objFile.Folder + objFile.FileName;
            }
            else
                return "";
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            cMediaObject img = new cMediaObject();

            img.mediaObjectId = Convert.ToInt32(Request.QueryString["Id"]);
            img.mediaObjectCaption = teCaption.RichText.Text;
            img.mediaObjectUrl = image.Rows[0]["mediaObjectUrl"].ToString();
            img.mediaObjectTarget = this.txtTarget.Text;
            img.moduleId = ModuleId;            

            cMediaObjectController.updateMedia(img);

            Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "Edit", "mid=" + ModuleId));
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            int imgId = Convert.ToInt32(Request.QueryString["Id"]);

            cMediaObjectController.deleteMedia(imgId);

           Response.Redirect( Globals.NavigateURL( PortalSettings.ActiveTab.TabID, "Edit", "mid=" + ModuleId ) );
        }
    }

}